package com.baoyouqun.controller.back;

import cn.hutool.core.lang.tree.Tree;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.Query.LeopardFriendDistQuery;
import com.baoyouqun.domain.VO.LeopardFriendCoreStatVO;
import com.baoyouqun.domain.VO.NewUserDynamicVO;
import com.baoyouqun.domain.VO.VipDynamicVO;
import com.baoyouqun.service.LeopardFriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("back/leopardFriend")
@Api(tags = "后台_豹友数据管理接口")
public class LeopardFriendController {


    @Resource
    private LeopardFriendService leopardFriendService;

    @GetMapping("coreStat")
    @ApiOperation("获取豹友核心统计数据（总人数/在线/VIP/收入等）")
    public SingleResponse<LeopardFriendCoreStatVO> getCoreStat() {
        return leopardFriendService.getCoreStat();
    }

    @GetMapping("areaUserStat")
    @ApiOperation("地区用户统计")
    public SingleResponse<List<Tree<String>>> getAreaUserStat() {
        // 处理入参：去除空格，统一空字符串为“全国”标识
        return leopardFriendService.getAreaUserStat();
    }

    @GetMapping("demandDistByType")
    @ApiOperation("获取按类型统计的需求分布（发布需求/发布资源）")
    public SingleResponse<List<Tree<String>>> getDemandDistByType() {
        return leopardFriendService.getDemandDistByType();
    }

    @GetMapping("vipDynamic")
    @ApiOperation("获取开通VIP会员动态（前500条，按时间倒序）")
    public PageResponse<VipDynamicVO> getVipDynamic(@Valid LeopardFriendDistQuery query) {
        SingleResponse<IPage<VipDynamicVO>> response = leopardFriendService.getVipDynamic(query);
        IPage<VipDynamicVO> page = response.getData();
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("newUserDynamic")
    @ApiOperation("获取新豹友动态（前500条，按注册时间倒序）")
    public PageResponse<NewUserDynamicVO> getNewUserDynamic(@Valid LeopardFriendDistQuery query) {
        SingleResponse<IPage<NewUserDynamicVO>> response = leopardFriendService.getNewUserDynamic(query);
        IPage<NewUserDynamicVO> page = response.getData();
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("exportAreaDistExcel")
    @ApiOperation("导出豹友地区分布Excel")
    public void exportAreaDistExcel(@Valid String cityCode, HttpServletResponse response) throws IOException {
        leopardFriendService.exportAreaDistExcel(cityCode, response);
    }

    @GetMapping("exportDemandDistExcel")
    @ApiOperation("导出需求分布Excel")
    public void exportDemandDistExcel(@Valid LeopardFriendDistQuery query, HttpServletResponse response) throws IOException {
        leopardFriendService.exportDemandDistExcel(query, response);
    }

    @GetMapping("exportVipDynamicExcel")
    @ApiOperation("导出VIP会员动态Excel")
    public void exportVipDynamicExcel(@Valid LeopardFriendDistQuery query, HttpServletResponse response) throws IOException {
        leopardFriendService.exportVipDynamicExcel(query, response);
    }

    @GetMapping("exportNewUserDynamicExcel")
    @ApiOperation("导出新豹友动态Excel")
    public void exportNewUserDynamicExcel(@Valid LeopardFriendDistQuery query, HttpServletResponse response) throws IOException {
        leopardFriendService.exportNewUserDynamicExcel(query, response);
    }

}